package com.example.manager;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;

import ultility.dataString;

import com.example.manager.PauseableTimerHandler;
import android.util.Log;

import com.example.fruit.R;
import com.example.scene.GameScene;

public class LevelManager {
	public static int currentLevel = 1;
	public static boolean endingLevel = false;
	public static String text= "AB";//"ABCDEFGHIJKMN";
	
	GameScene mGameScene;
	public static final LevelManager INSTANCE = new LevelManager();
	public LevelManager() {
		mGameScene = (GameScene) SceneManager.getInstance().getCurrentScene();
	}
	public static LevelManager getInstance(){
		return INSTANCE;
	}
	public void runLevel1()
	{
		
	}
	public void goToNextLevel()
	{
		if (endingLevel) {
			Log.d("pos","go to next level");
			currentLevel++;
			if (currentLevel == 1) {
				runLevel1();
			} else if (currentLevel == 2) {
				Log.d("pos","go to next level2");
				runLevel2();
			}
		}
		endingLevel = false;
	}
	PauseableTimerHandler timer, timer1, timer2, timer3, timer4, timer5,
	timer6, timer7, timer8, timer9;
	int count = 0;
	int count1 = 0;
	int count2 = 0;
	int count3 = 0;
	int count4 = 0;
	int count5 = 0;

	int count6 = 0;
	int count8 = 0;
	int count7 = 0;
	int count9 = 0;
	public void runLevel2()
	{
	//	resetUnregisterTimerHandler();
		
		endingLevel = false;
		final float[] delays = dataString.getDelayPeriodFromResource(R.array.delay1);//convertStringToFloat(mGameScene.getResources().getStringArray(R.array.delay1));
		final float[] periods = dataString.getDelayPeriodFromResource(R.array.period1);//convertStringToFloat(activity.getResources().getStringArray(R.array.period1));
		
		 timer = new com.example.manager.PauseableTimerHandler(delays[0], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped)
				{
					Log.d("pos","timer is running");
				//	isContinuos = false;
					mGameScene.addText();
					//delay += period;
					timer.setTimerSeconds(periods[0]);
					count++;
					if (count == 1) {//2
						//engine.unregisterUpdateHandler(timer);
					//	timer.setTimerCallbackTriggered(true);
						timer.setAutoReset(false);
					}
				}			
			}
		});
		 
		mGameScene.registerUpdateByTimerHandler(timer);
		
		timer1 = new PauseableTimerHandler(delays[1], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer1 is running");
					//isContinuos = false;
					mGameScene.addText();mGameScene.addText();
				//	delay1 += period1;
					timer1.setTimerSeconds(periods[1]);
					count1++;
					if (count1 == 1) {//2
						//engine.unregisterUpdateHandler(timer1);
						//timer1.setTimerCallbackTriggered(true);
						timer1.setAutoReset(false);
					}
				}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer1);
		timer2 = new PauseableTimerHandler(delays[2], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer2 is running");
					//isContinuos = false;
					mGameScene.addBoom();
					//engine.unregisterUpdateHandler(timer2);
					//timer2.setTimerCallbackTriggered(true);
					timer2.setAutoReset(false);
				}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer2);
		
		timer3 = new PauseableTimerHandler(delays[3], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer3 is running");
				//	isContinuos = true;
					mGameScene.addText();
					//delay3 += period3;
					timer3.setTimerSeconds(periods[3]);
					count3++;
					if (count3 == 5) {//6
						//engine.unregisterUpdateHandler(timer3);
						//timer3.setTimerCallbackTriggered(true);
						timer3.setAutoReset(false);
					}
				}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer3);
		
		timer4 = new PauseableTimerHandler(delays[4], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer4 is running");
					//isContinuos = false;
					mGameScene.addText();
					mGameScene.addBoom();
					//delay4 += period4;
					timer4.setTimerSeconds(periods[4]);
					count4++;
					if (count4 == 1){//2
						//engine.unregisterUpdateHandler(timer4);
						//timer4.setTimerCallbackTriggered(true);
						timer4.setAutoReset(false);
					}
				}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer4);
		
		timer5 = new PauseableTimerHandler(delays[5], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer5 is running");
					//isContinuos = false;
					if (count5 == 0) {
						mGameScene.addText();
						mGameScene.addText();
					}
					if (count5 == 1) {
						mGameScene.addText();
						mGameScene.addText();
						mGameScene.addText();

					}
					if (count5 == 2) {
						mGameScene.addText();
						mGameScene.addText();
					}
					//delay5 += period5;
					timer5.setTimerSeconds(periods[5]);
					count5++;
					if (count5 == 2){//3
						//engine.unregisterUpdateHandler(timer5);
						//timer5.setTimerCallbackTriggered(true);
						timer5.setAutoReset(false);
					}
				}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer5);
		timer6 = new PauseableTimerHandler(delays[6], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer6 is running");
					//isContinuos = true;
					mGameScene.addTwoSideText();
					mGameScene.addTwoSideText();timer6.setTimerSeconds(periods[6]);
					count6++;
					if (count6 == 6) {//7
						//engine.unregisterUpdateHandler(timer6);
						//timer6.setTimerCallbackTriggered(true);
						timer6.setAutoReset(false);
					}
				}
			} 
		});
		mGameScene.registerUpdateByTimerHandler(timer6);
		timer7 = new PauseableTimerHandler(delays[7], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer7 is running");
					//isContinuos = false;
					mGameScene.addText();
					mGameScene.addText();
					mGameScene.addText();timer7.setTimerSeconds(periods[7]);
					//engine.unregisterUpdateHandler(timer7);
					//timer7.setTimerCallbackTriggered(true);
					timer7.setAutoReset(false);
			}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer7);
		timer8 = new PauseableTimerHandler(delays[8], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer8 is running");
					//isContinuos = false;
					mGameScene.addText();
					mGameScene.addText();timer8.setTimerSeconds(periods[8]);
					count8++;
					if (count8 == 1)//2
					{
						timer8.setAutoReset(false);
						//timer8.setTimerCallbackTriggered(true);
						//engine.unregisterUpdateHandler(timer8);
					}
			}
			}
		});
		mGameScene.registerUpdateByTimerHandler(timer8);
		timer9 = new PauseableTimerHandler(delays[9], true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//if (!isStopped) 
				{
					Log.d("pos","timer9 is running");
				//	isContinuos = false;
					mGameScene.addText();
					mGameScene.addText();
					mGameScene.addBoom();
					mGameScene.addText();timer9.setTimerSeconds(periods[9]);
					count9++;
					if (count9 == 4) {//5
						//engine.unregisterUpdateHandler(timer9);
						//timer9.setTimerCallbackTriggered(true);
						timer9.setAutoReset(false);
						endingLevel = true;
					}
				}
			}
		}); 
		mGameScene.registerUpdateHandler(timer9);
	}
	private void resetUnregisterTimerHandler() {
		timer.reset();
		timer1.reset();
		timer2.reset();
		timer3.reset();
		timer4.reset();
		timer5.reset();
		timer6.reset();
		timer7.reset();
		timer8.reset();
		timer9.reset();
		timer = null;
		timer1 = null;
		timer2 = null;
		timer3 = null;
		timer4 = null;
		timer5 = null;
		timer6 = null;
		timer7 = null;
		timer8 = null;
		timer9 = null;

		mGameScene.unregisterUpdateByTimerHandler(timer);
		mGameScene.unregisterUpdateByTimerHandler(timer1);
		mGameScene.unregisterUpdateByTimerHandler(timer2);
		mGameScene.unregisterUpdateByTimerHandler(timer3);
		mGameScene.unregisterUpdateByTimerHandler(timer4);
		mGameScene.unregisterUpdateByTimerHandler(timer5);
		mGameScene.unregisterUpdateByTimerHandler(timer6);
		mGameScene.unregisterUpdateByTimerHandler(timer7);
		mGameScene.unregisterUpdateByTimerHandler(timer8);
		mGameScene.unregisterUpdateByTimerHandler(timer9);
	}
}
